$(function(){
    new QWebChannel(qt.webChannelTransport, function(channel) {
        window.contactObject = channel.objects.contactObject;
        window.commonObject = channel.objects.commonObject;
        window.mainObject = channel.objects.mainObject;
        window.msgObject = channel.objects.msgObject;
        console.log(contactObject);

        //获取个人信息
        commonObject.getMyInfo(function(data){
            console.log('myinfo',data);
            initMyInfo(data);
        })

        //好友列表刷新通知
        contactObject.friendListRefreshSignal.connect(function(list){
            result = JSON.parse(list);
            refreshFriendList(result);
        })

        mainObject.readyForData();

        contactObject.addFriendNotifySignal.connect(function(data){
            data = JSON.parse(data)
            console.log('好友申请',data);
            newFriendApplyNotify(data);
        })

        //审核好友通知
        contactObject.examineFriendNotifySignal.connect(function(userId,status){
            console.log('审核好友通知',userId,status);
            changeExamineResult(userId,status);
        })

        //收到新消息
        msgObject.receiveMsgNotifySignal.connect(function(msg){
            receiveMsg(msg);
        });

        //ack
        msgObject.ackMsgNotifySignal.connect(function (ack) {
            ackMsg(ack);
        });


    })

    $('#search-btn').on('click',function () {
        contactObject.openAddFriendView();
    })

    //切换消息和联系人
    $('#content-em-parent a').on('click',function(){
        $(this).addClass('active');
        $(this).siblings().removeClass('active');
        $('#'+$(this).attr('id')+'-content').show();
        $('#'+$(this).attr('id')+'-content').siblings().hide();

    })

    //展开或关闭新好友选项卡
    $('.contact-group-em').on('click', function() {
        ulId = $(this).attr('id').replace('em','ul');
        ul = $('#' + ulId);
        if (!$(this).hasClass('iconn-26')) {
            $(this).addClass('iconn-26');
            $(ul).show();
        } else {
            $(this).removeClass('iconn-26');
            $('#new-friend-contentt').addClass('hideList');
            $(ul).hide();
        }
    });

    //绑定好友申请通知点击事件
    $('#new-friend-ul').on('click','li',function(){
        openFriendApplyDetail(this);
    });

    //好友列表点击事件
    $('#friend-ul').on('click','li',function(){
        openFriendDetail(this);
    });

    //点击某个回话事件
    $('#session').on('click','li',function(){
        openSession(this);
    });

    //发送消息
    $('#sendBtn').on('click',function(){
       sendMsg();
    });
    $('#input-content').on('keydown',function(e){
        if(e.keyCode == 13){
            e.preventDefault();
            sendMsg();
        }
    });

    $('#selectPic').on('click',function(){
        msgObject.openFileSelect();
    });

});

function initMyInfo(data){
    myInfo = JSON.parse(data)
    $('#myInfo').find('img').attr('src',myInfo.avatar);
}
